import React from 'react';
import FormEnhance, {InputDate, InputText} from "common/form";
import {Report} from "type/entity/Report";
import {addReport, fetchAddReport} from "storage/redux/reducer/report";
import dayjs from 'dayjs';
import {MessageInstance} from "antd/es/message/interface";
import {Dispatch, UnknownAction} from "@reduxjs/toolkit";

const ReportAdd = () => {
    const addSuccess = (value: Report | number, message: MessageInstance, dispatch: Dispatch<UnknownAction>) => {
        if (typeof value === "object" && "id" in value){
            dispatch(addReport(value));
        }
    };
    return (
        <FormEnhance<Report>
            title={"添加报销单"}
            backupText={"报销单列表"}
            requestAction={fetchAddReport}
            addSuccess={addSuccess}
            formDataConverter={e => {
                if (dayjs.isDayjs(e.createDate)){
                  e.createDate = e.createDate.format("YYYY-MM-DD");
                }
                return e;
            }}
            initialValues={{
                createDate: dayjs()
            }}
        >
            <InputText<Report> name={"explain"} label={"报销单说明"}/>
            <InputDate<Report> name={"createDate"} label={"创建日期"} required/>
        </FormEnhance>
    );
};

export default ReportAdd;